﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace lu596.HT
{
    public partial class frmQuanLyCap : Form
    {
        private int currentRowTONGDAI = -1;
        private int currentRowCAP = -1;
        private int currentRowNHOM_DOI = -1;

        private int currentRowNEXTCAP = -1;
        private int currentRowNEXTNHOM_DOI = -1;
        
        public frmQuanLyCap()
        {
            InitializeComponent();
        }

        private void frmQuanLyCap_Load(object sender, EventArgs e)
        {
            // TODO: This line of code loads data into the 'thietBiDataSet.DAY_DOI' table. You can move, or remove it, as needed.
            this.dAY_DOITableAdapter.Fill(this.thietBiDataSet.DAY_DOI);
            // TODO: This line of code loads data into the 'thietBiDataSet.NHOM_DOI' table. You can move, or remove it, as needed.
            this.nHOM_DOITableAdapter.Fill(this.thietBiDataSet.NHOM_DOI);
            // TODO: This line of code loads data into the 'thietBiDataSet.DUNG_LUONG_CAP' table. You can move, or remove it, as needed.
            this.dUNG_LUONG_CAPTableAdapter.Fill(this.thietBiDataSet.DUNG_LUONG_CAP);
            // TODO: This line of code loads data into the 'thietBiDataSet.tblTHIETBI' table. You can move, or remove it, as needed.
            this.tblTHIETBITableAdapter.Fill(this.thietBiDataSet.tblTHIETBI);
            // TODO: This line of code loads data into the 'thietBiDataSet.tblTRAM' table. You can move, or remove it, as needed.
            this.tblTRAMTableAdapter.Fill(this.thietBiDataSet.tblTRAM);
            // TODO: This line of code loads data into the 'thietBiDataSet.tblTHIETBI_TRAM' table. You can move, or remove it, as needed.
            this.tblTHIETBI_TRAMTableAdapter.Fill(this.thietBiDataSet.tblTHIETBI_TRAM);

            // TODO: This line of code loads data into the 'thietBiDataSet.TU_HOP' table. You can move, or remove it, as needed.
            this.tU_HOPTableAdapter.Fill(this.thietBiDataSet.TU_HOP);
            // TODO: This line of code loads data into the 'thietBiDataSet.CAP' table. You can move, or remove it, as needed.
            this.cAPTableAdapter.Fill(this.thietBiDataSet.CAP);

        }

        
        private void dataGridViewTONGDAI_CurrentCellChanged(object sender, EventArgs e)
        {
            if (currentRowTONGDAI != dataGridViewTONGDAI.CurrentCell.RowIndex)
            {
                currentRowTONGDAI = dataGridViewTONGDAI.CurrentCell.RowIndex;
                tUHOPDAUBindingSource.Filter = "THIETBI_TRAM_ID=" + dataGridViewTONGDAI.CurrentRow.Cells[0].Value.ToString();
                tUHOPDAUIDDataGridViewTextBoxColumn.DataSource = tUHOPDAUBindingSource;
                tUHOPCUOIBindingSource.Filter = "THIETBI_TRAM_ID=" + dataGridViewTONGDAI.CurrentRow.Cells[0].Value.ToString();
                tUHOPCUOIIDDataGridViewTextBoxColumn.DataSource = tUHOPCUOIBindingSource;
                cAPBindingSource.Filter = "THIETBI_TRAM_ID=" + dataGridViewTONGDAI.CurrentRow.Cells[0].Value.ToString();
                dataGridViewCAP.DataSource = cAPBindingSource;
                currentRowCAP = -1;
                if (dataGridViewCAP.Rows.Count > 0)
                {
                    dataGridViewCAP.Rows[0].Selected = true;
                    dataGridViewCAP_CurrentCellChanged(null, null);
                }
            }
        }

        
        private void dataGridViewCAP_CurrentCellChanged(object sender, EventArgs e)
        {
            if (dataGridViewCAP.CurrentCell != null)
            {
                if (currentRowCAP != dataGridViewCAP.CurrentCell.RowIndex)
                {
                    currentRowCAP = dataGridViewCAP.CurrentCell.RowIndex;
                    nEXTCAPBindingSource.Filter = "TU_HOP_DAU_ID=" + dataGridViewCAP.CurrentRow.Cells[4].Value.ToString();
                    dataGridViewNEXTCAP.DataSource = nEXTCAPBindingSource;
                    currentRowNEXTCAP = -1;
                    //Show NEXT CAP rows
                    if (dataGridViewNEXTCAP.Rows.Count > 0)
                    {
                        dataGridViewNEXTCAP.Rows[0].Selected = true;
                        dataGridViewNEXTCAP_CurrentCellChanged(null, null);
                    }
                    //View NHOM_DOI
                    nHOMDOIBindingSource.Filter = "CAP_ID=" + dataGridViewCAP.CurrentRow.Cells[0].Value.ToString();
                    dataGridViewNHOM_DOI.DataSource = nHOMDOIBindingSource;
                    currentRowNHOM_DOI = -1;
                    //Show DAY_DOI rows
                    if (dataGridViewNHOM_DOI.Rows.Count > 0)
                    {
                        dataGridViewNHOM_DOI.Rows[0].Selected = true;
                        dataGridViewNHOM_DOI_CurrentCellChanged(null, null);
                    }

                    //View statistical results
                    ThietBiDataSet.CAPDataTable objCap = new ThietBiDataSet.CAPDataTable();
                    List<int> result = objCap.GetDAYDOIInfoByCAP((int)dataGridViewCAP.CurrentRow.Cells[0].Value);
                    txtTongSoDayDoi.Text = result[0].ToString();
                    txtSoDoiDaKetNoi.Text = result[1].ToString();
                    txtSoDoiChuaKetNoi.Text = result[2].ToString();
                }
            }
        }

        
        private void dataGridViewNHOM_DOI_CurrentCellChanged(object sender, EventArgs e)
        {
            if (dataGridViewNHOM_DOI.CurrentCell != null)
            {
                if (currentRowNHOM_DOI != dataGridViewNHOM_DOI.CurrentCell.RowIndex)
                {
                    currentRowNHOM_DOI = dataGridViewNHOM_DOI.CurrentCell.RowIndex;
                    dAYDOIBindingSource.Filter = "NHOM_DOI_ID=" + dataGridViewNHOM_DOI.CurrentRow.Cells[0].Value.ToString();
                    dataGridViewDAY_DOI.DataSource = dAYDOIBindingSource;
                }
            }
        }

        private void dataGridViewNEXTCAP_CurrentCellChanged(object sender, EventArgs e)
        {
            if (dataGridViewNEXTCAP.CurrentCell != null)
            {
                if (currentRowNEXTCAP != dataGridViewNEXTCAP.CurrentCell.RowIndex)
                {
                    currentRowNEXTCAP = dataGridViewNEXTCAP.CurrentCell.RowIndex;

                    //View NHOM_DOI
                    nEXTNHOMDOIBindingSource.Filter = "CAP_ID=" + dataGridViewNEXTCAP.CurrentRow.Cells[0].Value.ToString();
                    dataGridViewNEXTNHOM_DOI.DataSource = nEXTNHOMDOIBindingSource;
                    currentRowNEXTNHOM_DOI = -1;
                    //Show DAY_DOI rows
                    if (dataGridViewNEXTNHOM_DOI.Rows.Count > 0)
                    {
                        dataGridViewNEXTNHOM_DOI.Rows[0].Selected = true;
                        dataGridViewNEXTNHOM_DOI_CurrentCellChanged(null, null);
                    }

                    //Get số đôi có thể kết nối
                    ThietBiDataSet.DUNG_LUONG_CAPDataTable tableDungLuongCap = new ThietBiDataSet.DUNG_LUONG_CAPDataTable();
                    DataView view = tableDungLuongCap.GetDungLuongCap((int)dataGridViewNEXTCAP.CurrentRow.Cells[5].Value);
                    txtSoDoiKetNoi.Text = view[0]["DUNG_LUONG"].ToString();

                }
            }
        }

        private void btnKetNoi_Click(object sender, EventArgs e)
        {
            int CAP_ID_TRUOC = (int)dataGridViewCAP.CurrentRow.Cells[0].Value;
            int CAP_ID = (int)dataGridViewNEXTCAP.CurrentRow.Cells[0].Value;
            int num = int.Parse(txtSoDoiKetNoi.Text);
            ThietBiDataSet.DAY_DOIDataTable objDAY_DOI = new ThietBiDataSet.DAY_DOIDataTable();
            objDAY_DOI.KetNoiDAYDOI(CAP_ID_TRUOC, CAP_ID, num);
            dAY_DOITableAdapter.Fill(thietBiDataSet.DAY_DOI);
        }

        private void dataGridViewNEXTNHOM_DOI_CurrentCellChanged(object sender, EventArgs e)
        {
            if (dataGridViewNEXTNHOM_DOI.CurrentCell != null)
            {
                if (currentRowNEXTNHOM_DOI != dataGridViewNEXTNHOM_DOI.CurrentCell.RowIndex)
                {
                    currentRowNEXTNHOM_DOI = dataGridViewNEXTNHOM_DOI.CurrentCell.RowIndex;
                    nEXTDAYDOIBindingSource.Filter = "NHOM_DOI_ID=" + dataGridViewNEXTNHOM_DOI.CurrentRow.Cells[0].Value.ToString();
                    dataGridViewNEXTDAY_DOI.DataSource = nEXTDAYDOIBindingSource;
                }
            }
        }
    }
}